<?php

namespace app\api\model;

use app\common\exception\BaseException;
use app\common\model\Company as CompanyModel;
use think\Collection;

/**
 * 企业模型
 * Class Company
 * @package app\api\model
 */
class Company extends CompanyModel
{
    /**
     * 追加字段
     * @var array
     */
    protected $append = [
    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'wxapp_id'
    ];

    /**
     * 企业介绍：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getCompanyContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    public function getViewTimeAttr($value, $data)
    {
        return date('Y-m-d', $data['create_time']);
    }


    /**
     * 企业详情
     * @param $Company_id
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function detail($Company_id)
    {

        $model = self::with(['image', 'category', 'banner', 'team', 'user'])
        ->alias('c')
        ->field('c.*,su.add_date')
        ->join('user u', 'c.user_id = u.user_id', 'LEFT')
        ->join('shanghui_user su', 'c.user_id = su.user_id and u.shanghui_id= su.shanghui_id', 'LEFT')
        ->where("c.company_id", '=', $Company_id)->find();
        if (!$model) {
            throw new BaseException(['msg' => '企业不存在']);
        }
        return $model;
    }

    /**
     * 获取企业列表
     * @param int $category_id
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($category_id = 0, $team_id = 0, $searchInput = "", $limit = 100000, $user_id = 0,$shanghui_id)
    {
//        $category_id > 0 && $this->where('category_id', '=', $category_id);
//        $team_id > 0 && $this->where('team_id', '=', $team_id);
//        $searchInput != "" && $this->where('Company_name', 'like', '%' . $searchInput . '%');
        $obj = $this->with(['image', 'category', "team", "user"])
            ->alias('c')
            ->field('c.*,su.add_date')
            ->join('user u', 'c.user_id = u.user_id', 'LEFT')
            ->join('shanghui_user su', 'c.user_id = su.user_id', 'LEFT')
            ->join('team t', 'c.team_id = t.team_id', 'LEFT')
            ->where('c.company_status', '=', 1)
            ->where('c.is_delete', '=', 0)
            ->where('su.shanghui_id', '=', $shanghui_id);
        if ($category_id > 0) {
            $obj->where('c.category_id', '=', $category_id);
        }
        if ($team_id > 0) {
            $obj->where('c.team_id', '=', $team_id);
        }
        if ($searchInput != "") {
            if (is_numeric(str_replace('年', '', $searchInput))) {
                $year = str_replace('年', '', $searchInput);
                $start = date('Y') - $year . '-01-01';
                $end = date('Y') - $year . '-12-31';
                $obj->where('add_date','between time',[$start,$end]);
            } else {
                $obj->where('c.company_name|u.nickName|t.team_name', 'like', '%' . $searchInput . '%');
            }

        }
        if ($obj) {
            $obj = $obj->paginate($limit, false, ['query' => \request()->request()])->toArray();
        }
        return $obj;
    }

    /**
     * 关联封面图
     * @return \think\model\relation\HasOne
     */
    public function image()
    {
        return $this->hasOne('uploadFile', 'file_id', 'image_id');
    }

    /**
     * 分队
     * @return \think\model\relation\HasOne
     */
    public function team()
    {
        return $this->hasOne('team', 'team_id', 'team_id');
    }

    /**
     * 用户
     * @return \think\model\relation\HasOne
     */
    public function user()
    {
        return $this->hasOne('user', 'user_id', 'user_id');
    }

    /**
     * 关联分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        return $this->BelongsTo("\\app\\common\\model\\company\\Category", 'category_id', 'category_id');
    }


    /**
     * 关联企业banner图
     * @return \think\model\relation\HasOne
     */
    public function banner()
    {
        return $this->hasOne('uploadFile', 'file_id', 'banner_id');
    }

}